package service

import (
	"github.com/gin-gonic/gin"
	"mojieshi/define"
	"mojieshi/models"
	"net/http"
	"strconv"
	"strings"
)

// 获取服务商品详情页
func GetFuProductInfo(c *gin.Context) {
	var jsonData Newdata
	err := c.ShouldBindJSON(&jsonData)
	if err != nil {
		define.Response.ResultFail(c, 10000, define.JsonDataX, "")
		return
	}
	var proData models.MFuProduct
	err = models.DB.Model(new(models.MFuProduct)).Where("id=? AND aid=?", jsonData.Id, jsonData.Aid).First(&proData).Error
	if err != nil {
		define.Response.ResultFail(c, 10001, define.JsonDataX, "")
		return
	}
	var proJS ProJson

	proJS.Pic = proData.Pic
	proJS.Name = proData.Name
	proJS.SubName = proData.SubName
	proJS.ID = proData.ID
	proJS.SellPrice = proData.SellPrice
	proJS.MarketPrice = proData.MarketPrice
	proJS.Sales = proData.Sales
	proJS.Detail = proData.Detail
	proJS.Pics = proData.Pics
	proJS.Guige = proData.Guigedata
	proJS.Tag = proData.Tag
	proJS.Type = proData.Type
	proJS.Scoredkmaxset = proData.Scoredkmaxset
	proJS.Scoredkmaxval = proData.Scoredkmaxval
	//获取用户服务区域/或者配上区域
	if proData.Type == 1 {
		newArr := strings.Split(proData.Freightdata, ",")
		for _, v := range newArr {
			var regionData models.MRegion
			num, _ := strconv.Atoi(v)
			err = models.DB.Model(new(models.MRegion)).Where("id=?", num).First(&regionData).Error
			if err != nil {
				define.Response.ResultFail(c, 10005, define.JsonDataX, "")
				return
			}
			proJS.Region = append(proJS.Region, regionData)
		}

	}
	//获取规格
	var GuigeData []models.MShopGuigeFu
	err = models.DB.Model(new(models.MShopGuigeFu)).Where("proid=?", jsonData.Id).Find(&GuigeData).Error
	if err != nil {
		define.Response.ResultFail(c, 10002, define.JsonDataX, "")
		return
	}
	for _, v := range GuigeData {
		one := GuiGeData{
			ID:          v.ID,
			Proid:       v.Proid,
			Name:        v.Name,
			Pic:         v.Pic,
			MarketPrice: v.MarketPrice,
			SellPrice:   v.SellPrice,
			Sales:       v.Sales,
			Ks:          v.Ks,
			Barcode:     v.Barcode,
		}
		proJS.GuiGeList = append(proJS.GuiGeList, one)
	}
	//判断商品是否有推荐商品
	if proData.TjProid != "" {
		//有推荐商品
		var proIds = strings.Split(proData.TjProid, ",")
		for _, v := range proIds {
			num, _ := strconv.Atoi(v)
			var proOne models.MFuProduct
			err = models.DB.Model(new(models.MFuProduct)).Where("id=?", num).First(&proOne).Error
			if err != nil {
				define.Response.ResultFail(c, 10003, define.JsonDataX, "")
				return
			}
			proJS.TjFu = append(proJS.TjFu, proOne)
		}
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"code":    0,
		"data":    proJS,
	})
}
